package com.tools.ad.adscene;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;

import com.hm.ads.sdk.HmAdsSDK;
import com.hm.adsbuisiness.bean.HmAdPlace;
import com.hm.adsbuisiness.bean.HmAdUnionPlace;
import com.hm.adsbuisiness.bean.RawAd;
import com.hm.adsbuisiness.bean.nativead.RawNativeAd;
import com.hm.adsbuisiness.service.AdActionException;
import com.hm.adsbuisiness.utils.AdUtils;
import com.hm.adsui.card.AdCardEventListener;
import com.hm.adsui.card.CommonNativeAdCardView;
import com.hm.adsui.card.FullScreenAmNativeADCardView;
import com.hm.adsui.card.FullScreenNativeAdCardView;
import com.tools.ad.ADConstant;
import com.tools.ad.EntranceType;
import com.tools.ad.R;

import static com.tools.ad.AdType.AM_NATIVE;
import static com.tools.ad.AdType.AM_NEW_NATIVE;
import static com.tools.ad.AdType.DA_NATIVE;
import static com.tools.ad.AdType.FB_NATIVE;
import static com.tools.ad.AdType.MP_NATIVE;

/**
 * desc: 模拟 插屏广告（将native广告以插屏的形式展示）
 * date: 2018/11/12
 * author: ancun
 */
public class InterstitialOfNativeActivity extends InterstitialActivity {

    public static void showInterstitialAd(Context context, String entranceType, int placeId) {
        Intent intent = new Intent(context, InterstitialOfNativeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(ENTRANCE_TYPE, entranceType);
        intent.putExtra(PLACE_ID, placeId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ad_activity_native_to_interstitial);

        initHomeKeyReceiver();
        initAdView();
    }

    private void initAdView() {
        Intent intent = getIntent();
        if (intent != null) {
            String mEntranceTypeStr = intent.getStringExtra(ENTRANCE_TYPE);
            mPlaceId = intent.getIntExtra(PLACE_ID, ADConstant.INVALID_ID);

            if (mPlaceId == ADConstant.INVALID_ID) {
                close();
                return;
            }

            mEntranceType = EntranceType.enumOf(mEntranceTypeStr);
            HmAdUnionPlace mAdUnionPlace = HmAdsSDK.getInstance().getUnionAd(mPlaceId);
            final RawAd mActiveAd = ((AdStrategyScene) mEntranceType.getAdScene()).getActiveAd();

            if (mActiveAd == null) {
                close();
                return;
            }

            CommonNativeAdCardView cardView = null;
            if (AdUtils.isAdmob(mActiveAd)) {
                cardView = new FullScreenAmNativeADCardView(this);
            } else {
                cardView = new FullScreenNativeAdCardView(this);
            }

            try {
                cardView.bindRawAd(mAdUnionPlace, (RawNativeAd) mActiveAd);
            } catch (AdActionException e) {
                e.printStackTrace();
            }

            final AdStrategyScene adScene = (AdStrategyScene) mEntranceType.getAdScene();

            cardView.setEventListener(new AdCardEventListener() {
                @Override
                public void onAdClickClose(HmAdPlace adPlace) {

                    if (AdUtils.isFaceBook(mActiveAd)) {
                        adScene.onAdClose(FB_NATIVE);
                    } else if (AdUtils.isAdmob(mActiveAd)) {
                        if ("am".equals(mActiveAd.getPlatform())) {
                            adScene.onAdClose(AM_NATIVE);
                        } else {
                            adScene.onAdClose(AM_NEW_NATIVE);
                        }
                    } else if (AdUtils.isMopub(mActiveAd)) {
                        adScene.onAdClose(MP_NATIVE);
                    } else {
                        adScene.onAdClose(DA_NATIVE);
                    }

                    close();
                }

                @Override
                public void onAdCardShow(HmAdPlace adPlace) {

                    if (AdUtils.isFaceBook(mActiveAd)) {
                        adScene.onADShow(FB_NATIVE);
                    } else if (AdUtils.isAdmob(mActiveAd)) {
                        if ("am".equals(mActiveAd.getPlatform())) {
                            adScene.onADShow(AM_NATIVE);
                        } else {
                            adScene.onADShow(AM_NEW_NATIVE);
                        }
                    } else if (AdUtils.isMopub(mActiveAd)) {
                        adScene.onADShow(MP_NATIVE);
                    } else {
                        adScene.onADShow(DA_NATIVE);
                    }

                }

                @Override
                public void onTimeCount(HmAdPlace adPlace, int timecount) {
                    if (timecount <= 0) {
                        //close();
                    }
                }
            });

            //cardView.setFocusableInTouchMode(true);
            cardView.setOnKeyListener(new View.OnKeyListener() {
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    InterstitialOfNativeActivity.this.close();
                    return true;
                }
            });

            ViewGroup adContainer = findViewById(R.id.ad_native_container);
            adContainer.removeAllViews();
            adContainer.addView(cardView);
        }
    }

    @Override
    public void close() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
